/**
 * @desc : 常用表单指令
 * @author : xianggang
 * @date : 2019-08-31 15:10
 */
var app = angular.module( 'base');
// ab-parse-data
app.directive ( 'abParseData' , [ '$compile' , '$timeout' , '$injector' , function ( $compile , $timeout , $injector ) {

	return {
	    restrict : 'A' ,
	    scope : {
		    ngModel : "="
	    } ,
	    link : function ( scope , elm , attrs ) {

		    var json = attrs['uiParseData'];
		    if ( !json ) {
			    json = {
			        type : 'select' ,
			        format : 'string'
			    };
		    } else {
			    json = util.parseToJson ( json );
		    }

		    function formatData ( t ) {

			    switch ( t ) {
				    case "string" :
					    if ( scope.ngModel || scope.ngModel == 0 ) {
						    scope.ngModel = scope.ngModel + "";
					    }
				    break;
			    }
			    
		    }
		    
		    function setFormat ( ) {

			    switch ( json.type ) {
				    case "select" :
					    formatData ( json.format );
				    break;
			    }
			    
		    }
		    
		    if ( scope.ngModel ){
		    	setFormat ( );
		    }
		    scope.$watch ( "ngModel" , function ( newValue , oldValue ) {
			    $timeout ( function ( ) {
				    setFormat ( );
			    } );
		    } );
	    }
	};
} ] )
//ab-button-tools
.directive('abButtonTools', ['baseService',function(baseService) {
	return {
		restrict : 'AE',
		replace : true,
        transclude: true,
		scope:{parent:"=",list:"=",listKey:"=",index:"@",copy:"=",noSort:"=",sortKey:"=",noConfirm:"="},
		template :  '<div>'+
						'<span ng-transclude></span>'+
						/*'<a class="btn glyphicon glyphicon-move wd10" href="javascript:;" title="拖拽"></a>'+*/
						/*'<a class="btn glyphicon glyphicon-arrow-up wd10" href="javascript:;" ng-click="service.moveTr(list,index,true)" title="上移"></a>'+
						'<a class="btn glyphicon glyphicon-arrow-down violet wd10" href="javascript:;" ng-click="service.moveTr(list,index,false)" title="下移"></a>'+*/
						'<a class="btn fa fa-camera green wd10" href="javascript:;" ng-click="copyTr(list,index)" title="复制" ng-if="copy"></a>'+
						'<a class="btn fa fa-remove red wd10" href="javascript:;" ng-click="delTr(list,index,!noConfirm)" title="删除" ng-dbclick="delTr(list,index)"></a>'+
					'</div>',
		link : function(scope, elm, attrs) {
			scope.service = baseService;
			scope.pTr = elm.parents("tr");
			scope.pTds = scope.pTr.find("td");
			scope.pTbody = elm.parents("tbody").eq(0);
			scope.tdWidth = [];
			scope.sortKey = scope.sortKey || "sn";
			scope.delTr = function(list,index,needConfirm){
				if(needConfirm){
					$.Dialog.confirm("温馨提示","确认删除吗？", function() {
						scope.$apply(function(){
							window.sort.delTr(list,index);
						});
					});
					
				}else{
					window.sort.delTr(list,index);
				}
			};
			scope.copyTr = function(list,index){
				index = parseInt(index);
				list.insert(index,angular.copy(list[index]));
				window.sort.sortListBySn(scope.list,scope.sortKey);
			};
			if(scope.noSort) return;
			//控制宽度
			scope.pTds.each(function(i,e){
				scope.tdWidth.push($(e).width());
			});
			var list = scope.list;
			// Sortable 插件
			scope.sortEl = Sortable.create(scope.pTbody[0], {
                ghostClass: "sortable-ghost",  // Class name for the drop placeholder
                chosenClass: "sortable-chosen",  // Class name for the chosen item
                dragClass: "sortable-drag",  // Class name for the dragging item
                onStart: function (/**Event*/evt) {
                },
                onEnd: function (/**Event*/evt) {
                	
//                    var list = scope.list;
//                    // 重新设置排序编码
//                    listEl.find("tr").each(function (i, e) {
//                        list[$(this).attr("sn")][sortField] = i + 1;
//                    });
//                    util.sortListBySn(list, sortField);
//                    !scope.$$phase && scope.$digest();parentList
                	var str = "scope.parent." + scope.listKey;
                	var parentList = eval ( "(" + str + ")" );
                	if(parentList&&parentList!=list){
                		list = parentList;
                	}
					scope.pTbody.find("tr").each(function(i,e){
						var sn = $(this).attr("tr_sn");
						if(list[sn]){
							list[sn][scope.sortKey || "sn"] = i + 1;
						}
					});
					window.sort.sortListBySn(list,scope.sortKey);
					scope.$root.$digest();
					scope.pTbody.find("tr").each(function(i,e){
						$(e).attr("tr_sn",i);
					});
                }
            });
			
			scope.pTbody.find("tr").each(function(i,e){
				$(e).attr("tr_sn",i).addClass("w-c-i");
			});
		}
	};
}])
//ab-icon-select
// <ab-icon-select ng-model="data.resIcon"></ab-icon-select>
.directive('abIconSelect', ['$injector',function($injector) {
    return {
        restrict : 'EA',
        replace : true,
        transclude: true,
        scope : {
            ngModel:"="
        },
        template:'<div class="inline-block">'+
			        '<button type="button" ng-click="openIconModal()" class="btn btn-primary"><i class="fa fa-eye"></i></button>'+
			        '<div class="modal fade" id="{{iconId}}" tabindex="-1" role="dialog" data-backdrop="static" aria-labelledby="myModalLabel" aria-hidden="true">'+
				        '<div class="modal-dialog" style="width:50%;">'+
					        '<div class="modal-content">'+
						        '<div class="modal-header t-c">'+
							        '<button type="button" class="close" ng-click="closeModal()">&times;</button>'+
							        '<h4 class="modal-title">图标选择器</h4>'+
							    '</div>'+
							    '<div class="modal-body">'+
							        '<input type="text" class="form-control" ng-model="searchIcon" placeholder="过滤..."/>'+
							        '<ul style="word-break: break-all;height: 600px;" class="o-y-s">'+
								        '<li ng-repeat="icon in iconList | filter: searchIcon track by $index" class="inline-block fl ml10 pointer" ng-click="$parent.tempSelectIcon=icon">'+
								        	'<span class="fa {{icon}} fs20 icons-noselect" ng-class="{\'icons-select\':$parent.tempSelectIcon==icon}"></span>'+
								        '</li>'+
							        '</ul>'+
							    '</div>'+
						        '<div class="modal-footer t-a-c">'+
							        '<button type="button" class="btn btn-default btn-outline" ng-click="closeModal()">关闭</button>'+
							        '<button type="button" class="btn btn-primary btn-outline" ng-click="saveIcon()">确认</button>'+
							    '</div>'+
					        '</div>'+
				        '</div>'+
			        '</div>'+
		        '</div>',
        link : function(scope, elm, attrs) {
            scope.iconId = "iconModal_"+ GetRandomNum(1,100000);
            scope.iconList = ['fa-adjust','fa-anchor','fa-archive','fa-area-chart','fa-arrows','fa-arrows-h','fa-arrows-v','fa-asterisk','fa-at','fa-automobile','fa-ban','fa-bank','fa-bar-chart','fa-bar-chart-o','fa-barcode','fa-bars','fa-bed','fa-beer','fa-bell','fa-bell-o','fa-bell-slash','fa-bell-slash-o','fa-bicycle','fa-binoculars','fa-birthday-cake','fa-bolt','fa-bomb','fa-book','fa-bookmark','fa-bookmark-o','fa-briefcase','fa-bug','fa-building','fa-building-o','fa-bullhorn','fa-bullseye','fa-bus','fa-cab','fa-calculator','fa-calendar','fa-calendar-o','fa-camera','fa-camera-retro','fa-car','fa-caret-square-o-down','fa-caret-square-o-left','fa-caret-square-o-right','fa-caret-square-o-up','fa-cart-arrow-down','fa-cart-plus','fa-cc','fa-certificate','fa-check','fa-check-circle','fa-check-circle-o','fa-check-square','fa-check-square-o','fa-child','fa-circle','fa-circle-o','fa-circle-o-notch','fa-circle-thin','fa-clock-o','fa-close','fa-cloud','fa-cloud-download','fa-cloud-upload','fa-code','fa-code-fork','fa-coffee','fa-cog','fa-cogs','fa-comment','fa-comment-o','fa-comments','fa-comments-o','fa-compass','fa-copyright','fa-credit-card','fa-crop','fa-crosshairs','fa-cube','fa-cubes','fa-cutlery','fa-dashboard','fa-database','fa-desktop','fa-diamond','fa-dot-circle-o','fa-download','fa-edit','fa-ellipsis-h','fa-ellipsis-v','fa-envelope','fa-envelope-o','fa-envelope-square','fa-eraser','fa-exchange','fa-exclamation','fa-exclamation-circle','fa-exclamation-triangle','fa-external-link','fa-external-link-square','fa-eye','fa-eye-slash','fa-eyedropper','fa-fax','fa-female','fa-fighter-jet','fa-file-archive-o','fa-file-audio-o','fa-file-code-o','fa-file-excel-o','fa-file-image-o','fa-file-movie-o','fa-file-pdf-o','fa-file-photo-o','fa-file-picture-o','fa-file-powerpoint-o','fa-file-sound-o','fa-file-video-o','fa-file-word-o','fa-file-zip-o','fa-film','fa-filter','fa-fire','fa-fire-extinguisher','fa-flag','fa-flag-checkered','fa-flag-o','fa-flash','fa-flask','fa-folder','fa-folder-o','fa-folder-open','fa-folder-open-o','fa-frown-o','fa-futbol-o','fa-gamepad','fa-gavel','fa-gear','fa-gears','fa-genderless','fa-gift','fa-glass','fa-globe','fa-graduation-cap','fa-group','fa-hdd-o','fa-headphones','fa-heart','fa-heart-o','fa-heartbeat','fa-history','fa-home','fa-hotel','fa-image','fa-inbox','fa-info','fa-info-circle','fa-institution','fa-key','fa-keyboard-o','fa-language','fa-laptop','fa-leaf','fa-legal','fa-lemon-o','fa-level-down','fa-level-up','fa-life-bouy','fa-life-buoy','fa-life-ring','fa-life-saver','fa-lightbulb-o','fa-line-chart','fa-location-arrow','fa-lock','fa-magic','fa-magnet','fa-mail-forward','fa-mail-reply','fa-mail-reply-all','fa-male','fa-map-marker','fa-meh-o','fa-microphone','fa-microphone-slash','fa-minus','fa-minus-circle','fa-minus-square','fa-minus-square-o','fa-mobile','fa-mobile-phone','fa-money','fa-moon-o','fa-mortar-board','fa-motorcycle','fa-music','fa-navicon','fa-newspaper-o','fa-paint-brush','fa-paper-plane','fa-paper-plane-o','fa-paw','fa-pencil','fa-pencil-square','fa-pencil-square-o','fa-phone','fa-phone-square','fa-photo','fa-picture-o','fa-pie-chart','fa-plane','fa-plug','fa-plus','fa-plus-circle','fa-plus-square','fa-plus-square-o','fa-power-off','fa-print','fa-puzzle-piece','fa-qrcode','fa-question','fa-question-circle','fa-quote-left','fa-quote-right','fa-random','fa-recycle','fa-refresh','fa-remove','fa-reorder','fa-reply','fa-reply-all','fa-retweet','fa-road','fa-rocket','fa-rss','fa-rss-square','fa-search','fa-search-minus','fa-search-plus','fa-send','fa-send-o','fa-server','fa-share','fa-share-alt','fa-share-alt-square','fa-share-square','fa-share-square-o','fa-shield','fa-ship','fa-shopping-cart','fa-sign-in','fa-sign-out','fa-signal','fa-sitemap','fa-sliders','fa-smile-o','fa-soccer-ball-o','fa-sort','fa-sort-alpha-asc','fa-sort-alpha-desc','fa-sort-amount-asc','fa-sort-amount-desc','fa-sort-asc','fa-sort-desc','fa-sort-down','fa-sort-numeric-asc','fa-sort-numeric-desc','fa-sort-up','fa-space-shuttle','fa-spinner','fa-spoon','fa-square','fa-square-o','fa-star','fa-star-half','fa-star-half-empty','fa-star-half-full','fa-star-half-o','fa-star-o','fa-street-view','fa-suitcase','fa-sun-o','fa-support','fa-tablet','fa-tachometer','fa-tag','fa-tags','fa-tasks','fa-taxi','fa-terminal','fa-thumb-tack','fa-thumbs-down','fa-thumbs-o-down','fa-thumbs-o-up','fa-thumbs-up','fa-ticket','fa-times','fa-times-circle','fa-times-circle-o','fa-tint','fa-toggle-down','fa-toggle-left','fa-toggle-off','fa-toggle-on','fa-toggle-right','fa-toggle-up','fa-trash','fa-trash-o','fa-tree','fa-trophy','fa-truck','fa-tty','fa-umbrella','fa-university','fa-unlock','fa-unlock-alt','fa-unsorted','fa-upload','fa-user','fa-user-plus','fa-user-secret','fa-user-times','fa-users','fa-video-camera','fa-volume-down','fa-volume-off','fa-volume-up','fa-warning','fa-wheelchair','fa-wifi','fa-wrench','fa-ambulance','fa-car','fa-bicycle','fa-bus','fa-taxi','fa-fighter-jet','fa-motorcycle','fa-plane','fa-rocket','fa-ship','fa-space-shuttle','fa-subway','fa-train','fa-truck','fa-wheelchair','fa-circle-thin','fa-mars','fa-mars-double','fa-mars-stroke','fa-mars-stroke-h','fa-mars-stroke-v','fa-mercury','fa-neuter','fa-transgender','fa-transgender-alt','fa-venus','fa-venus-double','fa-venus-mars','fa-file','fa-file-archive-o','fa-file-audio-o','fa-file-code-o','fa-file-excel-o','fa-file-image-o','fa-file-video-o','fa-file-o','fa-file-pdf-o','fa-file-powerpoint-o','fa-file-text','fa-file-text-o','fa-file-word-o','fa-cog','fa-circle-o-notch','fa-refresh','fa-spinner','fa-check-square','fa-check-square-o','fa-circle','fa-circle-o','fa-dot-circle-o','fa-minus-square','fa-minus-square-o','fa-plus-square','fa-plus-square-o','fa-square','fa-square-o','fa-cc-amex','fa-cc-discover','fa-cc-mastercard','fa-cc-paypal','fa-cc-stripe','fa-cc-visa','fa-credit-card','fa-google-wallet','fa-paypal','fa-area-chart','fa-bar-chart','fa-line-chart','fa-pie-chart','fa-btc','fa-jpy','fa-usd','fa-eur','fa-gbp','fa-ils','fa-inr','fa-krw','fa-money','fa-rub','fa-try','fa-align-center','fa-align-justify','fa-align-left','fa-align-right','fa-bold','fa-font','fa-italic','fa-header','fa-underline','fa-text-width','fa-text-height','fa-strikethrough','fa-subscript','fa-superscript','fa-link','fa-chain-broken','fa-clipboard','fa-files-o','fa-scissors','fa-columns','fa-eraser','fa-file','fa-file-o','fa-file-text','fa-file-text-o','fa-floppy-o','fa-repeat','fa-undo','fa-outdent','fa-indent','fa-list','fa-list-alt','fa-list-ol','fa-list-ul','fa-paperclip','fa-paragraph','fa-table','fa-th','fa-th-large','fa-th-list','fa-angle-double-down','fa-angle-double-left','fa-angle-double-right','fa-angle-double-up','fa-angle-down','fa-angle-left','fa-angle-right','fa-angle-up','fa-arrow-circle-down','fa-arrow-circle-left','fa-arrow-circle-right','fa-arrow-circle-up','fa-arrow-circle-o-down','fa-arrow-circle-o-left','fa-arrow-circle-o-right','fa-arrow-circle-o-up','fa-arrow-down','fa-arrow-left','fa-arrow-right','fa-arrow-up','fa-arrows','fa-arrows-alt','fa-arrows-h','fa-arrows-v','fa-caret-down','fa-caret-left','fa-caret-right','fa-caret-up','fa-caret-square-o-down','fa-caret-square-o-left','fa-caret-square-o-right','fa-caret-square-o-up','fa-chevron-circle-down','fa-chevron-circle-left','fa-chevron-circle-right','fa-chevron-circle-up','fa-chevron-down','fa-chevron-left','fa-chevron-right','fa-chevron-up','fa-hand-o-down','fa-hand-o-left','fa-hand-o-right','fa-hand-o-up','fa-long-arrow-down','fa-long-arrow-left','fa-long-arrow-right','fa-long-arrow-up','fa-arrows-alt','fa-backward','fa-compress','fa-eject','fa-expand','fa-fast-backward','fa-fast-forward','fa-forward','fa-pause','fa-play','fa-play-circle','fa-play-circle-o','fa-step-backward','fa-step-forward','fa-stop','fa-youtube-play','fa-volume-off','fa-volume-down','fa-volume-up','fa-random','fa-ambulance','fa-h-square','fa-heart','fa-heart-o','fa-heartbeat','fa-hospital-o','fa-medkit','fa-plus-square','fa-stethoscope','fa-user-md','fa-wheelchair','fa-adn','fa-android','fa-angellist','fa-apple','fa-behance','fa-behance-square','fa-bitbucket','fa-bitbucket-square','fa-btc','fa-buysellads','fa-cc-amex','fa-cc-discover','fa-cc-mastercard','fa-cc-paypal','fa-cc-stripe','fa-cc-visa','fa-codepen','fa-connectdevelop','fa-css3','fa-dashcube','fa-delicious','fa-deviantart','fa-digg','fa-dribbble','fa-dropbox','fa-drupal','fa-empire','fa-facebook','fa-facebook-official','fa-facebook-square','fa-flickr','fa-forumbee','fa-foursquare','fa-git','fa-git-square','fa-github','fa-github-alt','fa-github-square','fa-gratipay','fa-google','fa-google-plus','fa-google-plus-square','fa-google-wallet','fa-hacker-news','fa-html5','fa-instagram','fa-ioxhost','fa-joomla','fa-jsfiddle','fa-lastfm','fa-lastfm-square','fa-leanpub','fa-linkedin','fa-linkedin-square','fa-linux','fa-maxcdn','fa-meanpath','fa-medium','fa-openid','fa-pagelines','fa-paypal','fa-pied-piper','fa-pied-piper-alt','fa-pinterest','fa-pinterest-p','fa-pinterest-square','fa-qq','fa-rebel','fa-reddit','fa-reddit-square','fa-renren','fa-sellsy','fa-share-alt','fa-share-alt-square','fa-shirtsinbulk','fa-simplybuilt','fa-skyatlas','fa-skype','fa-slack','fa-slideshare','fa-soundcloud','fa-spotify','fa-stack-exchange','fa-stack-overflow','fa-steam','fa-steam-square','fa-stumbleupon','fa-stumbleupon-circle','fa-tencent-weibo','fa-trello','fa-tumblr','fa-tumblr-square','fa-twitch','fa-twitter','fa-twitter-square','fa-viacoin','fa-vimeo-square','fa-vine','fa-vk','fa-weixin','fa-weibo','fa-whatsapp','fa-windows','fa-wordpress','fa-xing','fa-xing-square','fa-yahoo','fa-yelp','fa-youtube','fa-youtube-play','fa-youtube-square'];
            
            scope.openIconModal = function(){
                $("#"+scope.iconId).modal("show");
            }
            scope.saveIcon = function(){
                scope.ngModel = scope.tempSelectIcon;
                $("#"+scope.iconId).modal("hide")
            }
            scope.closeModal = function(){
                $("#"+scope.iconId).modal("hide")
            }
        }
    };
}])